package com.example.springboot2022nocv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot2022nocv.entity.Menu;
import com.example.springboot2022nocv.entity.Role;
import com.example.springboot2022nocv.service.MenuService;
import com.example.springboot2022nocv.service.RoleService;
import com.example.springboot2022nocv.utils.TreeNode;
import com.example.springboot2022nocv.vo.DataView;
import com.example.springboot2022nocv.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;

    @RequestMapping("/toRole")
    public String toRole(){
        return "role/role";
    }
    /***
     * 查询所有的角色带有分页，带有查询条件
     */
    @RequestMapping( "loadAllRole")
    @ResponseBody
    public DataView loadAllRole(RoleVo rolevo){
        IPage<Role> page = new Page<>(rolevo.getPage(),rolevo.getLimit());
        QueryWrapper<Role> querywrapper = new QueryWrapper<>();
        querywrapper.like(StringUtils.isNotBlank(rolevo.getName()),"name",rolevo.getName());
        querywrapper.like(StringUtils.isNotBlank(rolevo.getRemark()),"remark",rolevo.getRemark());
        roleService.page(page,querywrapper);
        return new DataView(page.getTotal(),page.getRecords());
    }

    @RequestMapping( "initPermissionByRoleId")
    @ResponseBody
    public DataView initPermissionByRoleId(Integer roleId){
        //1.把所有的菜单权限查出来
        QueryWrapper<Menu> querywrapper = new QueryWrapper<>();
        List<Menu> allPermissions = menuService.list();
        //2.根据角色rid 【roleId】查询所有的菜单mid
        List<Integer> currentRolePermissions = roleService.queryAllpermissionByRid(roleId);
        //3.rid mid所有的ID,去查询菜单和角色的数据
        List<Menu> menus = null;
        //4.查询到mid rid
        if (currentRolePermissions.size() >0){
            querywrapper.in(  "id" , currentRolePermissions);
            menus =menuService.list(querywrapper);
        }else {
            menus = new ArrayList<>();
        }
        //5.返回前台的格式，带有层级关系TREENODE
        List<TreeNode> nodes = new ArrayList<>();
        for (Menu allPermission : allPermissions){
            String checkArr = "O";
            for (Menu currentPermission : menus){
                if (allPermission.getId().equals(currentPermission.getId())){
                    checkArr = "1";
                    break;
                }
            }
            Boolean spread =    (allPermission.getOpen()==null || allPermission.getOpen() == 1) ?true:false;
            nodes.add(new TreeNode(allPermission.getId(),allPermission.getPid(),allPermission.getTitle(),spread,checkArr));
        }

        return new DataView(nodes);
    }

    @RequestMapping("/saveRolePermission")
    @ResponseBody
    public DataView saveRolePermission( Integer rid,Integer[] mids){
        //1.根据rid删除之前的mid权限
        roleService.deleteRoleByRid(rid);
        //2.保存权限
        if (mids!=null && mids.length>0){
            for (Integer mid : mids){
                roleService.saveRoleMenu(rid,mid);
            }
        }
        DataView dataView = new DataView();
        dataView.setCode( 200);
        dataView.setMsg("菜单权限分配成功!");
        return dataView;
    }


}
